package com.niiwoo.civet.trade.report.client.report.common;

import org.springframework.util.CollectionUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @version 20180906
 * www.resoft.css.com.cn
 * 报表数据保存到txt压缩文件工具类
 * @author wanglei
 *
 */
public class ReportToTxtUtil {

	private String fileDirPath;
	
	private String companyCode;
	
	private String companyType;
	
	private String fileName;
	
	private String frequency;
	
	private String reportCode;
	
	private String reportDate;
	
    public String getFrequency() {
		return frequency;
	}

	public String getReportCode() {
		return reportCode;
	}

	public String getReportDate() {
		return reportDate;
	}
	
	/**
	 * 获取压缩后的zip文件路径
	 * @return
	 */
	public String getZipFilePath(){
		return fileDirPath+File.separator+fileName+".zip";
	}

	/**
	 * 构建机构信息
	 * @param fileDirPath	存储路径
	 * @param companyCode	企业社会统一信用代码
	 * @param companyType	企业类型 p2p xd jycs
	 */
	public ReportToTxtUtil(String fileDirPath, String companyCode,String companyType){
		this.fileDirPath = fileDirPath;
		this.companyCode = companyCode;
		this.companyType = companyType;
	}

	/**
	 * 初始化机构信息
	 * @param frequency		频度  d m q y
	 * @param reportCode	J1001 J1002 J1003 J1004 J1005 J1006
	 * @param reportDate	格式：20180931
	 */
	public void initReportInfo(String frequency,String reportCode, String reportDate){
		this.frequency = frequency;
		this.reportCode = reportCode;
		this.reportDate = reportDate;
		
		File fileDir = new File(fileDirPath);
		if(!fileDir.exists()) fileDir.mkdirs();
		fileName = companyCode+"_"+reportDate+"_"+companyType+"_"+frequency+"_"+reportCode+".txt";
		File zipFile = new File(fileDirPath+File.separator+fileName+".zip");
		if(zipFile.exists()) zipFile.delete();
	}
	/**
	 *
	 * @param zipOutputStream
	 * @param list<T extends Report>
	 * @throws IOException
	 */
	public <T extends Report> void dataWriteToZipFile(ZipOutputStream zipOutputStream,List<T> list) throws IOException{
		for(T report:list){
			dataWriteToZipFile(zipOutputStream,report.toString());
		}
	}
	
	/**
	  * 
	  * @param zipOutputStream
	  * @param list<T extends Report>
	  * @throws IOException
	  */
	 public <T extends Report> void dataWriteToZipFile1(ZipOutputStream zipOutputStream,List<T> list) throws IOException{
	  StringBuffer content = new StringBuffer();
	  if (CollectionUtils.isEmpty(list)){
		  System.out.println("文件为空，直接跳出");
		  return;
	  }
	  for(T report:list){
	      content.append(report.toString());
	  }
	  dataWriteToZipFile(zipOutputStream,content.toString());
	 }
	
	/**
	 * 保存数据到压缩文件中
	 * @param zipOutputStream
	 * @param content
	 * @throws IOException
	 */
	public void dataWriteToZipFile(ZipOutputStream zipOutputStream,String content) throws IOException{
    	ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content.getBytes("utf-8"));
    	BufferedInputStream bis = new BufferedInputStream(byteArrayInputStream);  
    	int len = 0;  
    	byte[] buffer = new byte[1024];  
    	while ((len = bis.read(buffer)) != -1) {  
    		zipOutputStream.write(buffer, 0, len);  
    	}  
    	zipOutputStream.flush();
    	bis.close();  
    	byteArrayInputStream.close();
    }
	
	/**
	 * 获取压缩流
	 * @return
	 * @throws IOException
	 */
	public ZipOutputStream getZipOutputStream() throws IOException{
		FileOutputStream	fileOutputStream = new FileOutputStream(new File(fileDirPath+File.separator+fileName+".zip"));
		ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));
		zipOutputStream.putNextEntry(new ZipEntry(fileName));
		return zipOutputStream;
    }
	
	/**
	 * 关闭压缩流
	 * @param zipOutputStream
	 * @throws IOException
	 */
	public void closeZipOutputStream(ZipOutputStream zipOutputStream) throws IOException{
		zipOutputStream.closeEntry();
		zipOutputStream.close();
    }
}
